package com.netty.cn.rpc.selfmessage;

import com.netty.cn.rpc.selfmessage.core.MyMessageDecoder;
import com.netty.cn.rpc.selfmessage.core.MyMessageEncoder;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class MyClient {
    public static void main(String[] args) {
    	int port=8080;
    	
        EventLoopGroup group=new NioEventLoopGroup();
        try {
            Bootstrap bootstrap=new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel socketChannel) {
                            ChannelPipeline channelPipeline=socketChannel.pipeline();
                            channelPipeline.addLast(new MyMessageDecoder());//加解码器
                            channelPipeline.addLast(new MyMessageEncoder());
                            channelPipeline.addLast(new MyClientHandler());
                        }
                    });
            //System.out.println("netty client start");
            //启动客户端连接服务器
            ChannelFuture cf =bootstrap.connect("127.0.0.1",port).sync();
            //关闭通道进行监听
            cf.channel().closeFuture().sync();
            System.out.println("启动客户端"+port);
        } catch(Exception e){
//        	log.error(e.toString(),e);
        	System.out.println(e.toString());
        }finally {
            group.shutdownGracefully();
        }
    }
}

